#include "Structs.h"
#include "Parse.h"
#include "Grammar.h"
#include "ModelBuilder.h"

#include <boost/foreach.hpp>

namespace GenParser
{	
	SemanticModel::ModelPtr parse(const std::wstring& source)
	{
		typedef std::wstring::const_iterator iterator_type;
		GenParser::GenGrammar<iterator_type> parser;
		iterator_type iter = source.begin();
		iterator_type end = source.end();
		
		ModelDef def;
		SemanticModel::ModelPtr result(new SemanticModel::Model());
		if (phrase_parse(iter, end, parser, ascii::space, def) && iter == end)
		{
			SemanticModelBuilder modelBuilder(result);
			BOOST_FOREACH(SemanticModelItem const& modelItem, def)
				boost::apply_visitor(modelBuilder, modelItem);
		}
		
		return result;
	}
}